#include "stdafx.h"
#include <CppUnitTest.h>

#include "nef.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

using namespace nef;

class IsVal 
{
public:
	typedef std::shared_ptr<IsVal> Ptr;

	IsVal()
		: WasImported(false)
	{
	}

	bool WasImported;
};

nef_export<IsVal> is_val_export;

class RealVal
	: public IPartImportsSatisfied
{
public:
	IsVal::Ptr Val;

	virtual void OnImportsSatisfied() 
	{
		Val->WasImported = true;
	}
};

template<>
struct nef::nef_traits<RealVal>
{
	typedef nef_export<RealVal> export0;

	typedef nef_import<RealVal, IsVal, &RealVal::Val> import0;
};

namespace nef_unit_test
{
	TEST_CLASS(imports_satisfied_tests)
	{
	public:
		TEST_METHOD(imports_satisfied_test)
		{
			auto cat = Catalog::Create();

			auto realVal = cat->GetExportedValue<RealVal>(NO_CONTRACT, false);

			auto val = cat->GetExportedValue<IsVal>(NO_CONTRACT, false);

			Assert::IsNotNull(realVal.get(), L"Could not locate object RealVal", LINE_INFO());
			Assert::IsNotNull(val.get(), L"Could not locate object Val", LINE_INFO());

			Assert::AreEqual(true, val->WasImported, L"The OnImportsSatisfied function was never invoked.", LINE_INFO());
		}
	};
}